package com.dsbj.mp.icardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.mp.icardservice.entity.Reservation;
import com.dsbj.mp.icardservice.entity.ReservationPrimary;
import com.dsbj.mp.icardservice.mapper.ReservationMapper;
import com.dsbj.mp.icardservice.mapper.ReservationPrimaryMapper;
import com.dsbj.mp.icardservice.utils.picprocess.Base64ImageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cjw 2019-10-09
 */
@Service
@Transactional
public class ReservationPrimaryService {

	@Resource
	private ReservationPrimaryMapper reservationPrimaryMapper;

	@Resource
	private ReservationMapper reservationMapper;
	
	/**
	 * 根据createUserEmployeeno分页获取预约单列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public Page<ReservationPrimary> getReservationPrimaryByPage(String createUserEmployeeno, int pagesize, int pagenum) {
		
		Page<ReservationPrimary> page = new Page<ReservationPrimary>().setSize(pagesize).setCurrent(pagenum);
		QueryWrapper<ReservationPrimary> queryWrapper = new QueryWrapper<ReservationPrimary>()
				.select("id", "RESERVATIONNO", "EMPLOYEENO_RECEPTION", "EMPLOYEENAME_RECEPTION", "FORECAST_VISITING_TIME", "VISITING_MATTERS", "CREATE_USER_EMPLOYEENO")
				.eq("CREATE_USER_EMPLOYEENO", createUserEmployeeno)
				.orderByDesc("CREATE_TIME");
		reservationPrimaryMapper.selectPage(page, queryWrapper);
		List<ReservationPrimary> reservationPrimaries = page.getRecords();
		
		/**
		 * 给reservationPrimary.visitorNames赋值。
		 */
		if (!CollectionUtils.isEmpty(reservationPrimaries)) {
			//获取相关的reservation，并根据reservationno分组。
			List<Reservation> reservations = reservationMapper.selectList(
					new QueryWrapper<Reservation>()
							.in("RESERVATIONNO",
									reservationPrimaries.stream()
											.map(ReservationPrimary::getReservationno).collect(Collectors.toList()))
							.orderByAsc("CREATE_TIME"));
			Map<String, List<Reservation>> groupMap = reservations.stream().collect(Collectors.groupingBy(Reservation::getReservationno));
			
			//给reservationPrimary.visitorNames赋值
			for(ReservationPrimary reservationPrimary: reservationPrimaries){
				List<Reservation> list_reservation=groupMap.get(reservationPrimary.getReservationno());
				
				if(list_reservation!=null && list_reservation.size()>0){
					String visitorNames=list_reservation.stream().map(Reservation::getVisitor).collect(Collectors.joining(","));
					reservationPrimary.setVisitorNames(visitorNames);
				}
			}
			/*
			reservationPrimaries.forEach(
					x -> x.setVisitorNames(
							groupMap.get(x.getReservationno())
									.stream()
									.map(Reservation::getVisitor)
									.collect(Collectors.joining(","))));
			*/
		}
		return page;
	}

	/**
	 * 将list_reservation存入reservationPrimary中。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public ReservationPrimary supplementReservationPrimaryWithReservation(ReservationPrimary reservationPrimary){
		if(reservationPrimary==null){
			return null;
		}
		List<Reservation> reservations= reservationMapper.selectList(
				new QueryWrapper<Reservation>()
						.eq("RESERVATIONNO",reservationPrimary.getReservationno())
						.orderByAsc("CREATE_TIME"));
		reservations.parallelStream().forEach(x->{
			if(StringUtils.isNoneBlank(x.getIdcardnoUrl())){
				String idcardnoStr_base64= Base64ImageUtil.getFileStrBase64_fromDownloadUrl(x.getIdcardnoUrl());
				x.setPhotoBase64(idcardnoStr_base64);
			}
		});
		reservationPrimary.setReservationList(reservations);
		reservationPrimary.setVisitorNames(reservations.stream().map(x->x.getVisitor()).collect(Collectors.joining(",")));
		return reservationPrimary;
	}

	/**
	 * 根据reservationPrimaryId获取访客详情。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public ReservationPrimary getReservationPrimaryById(String reservationPrimaryId){
		ReservationPrimary reservationPrimary=reservationPrimaryMapper.selectById(reservationPrimaryId);
		//将list_reservation存入reservationPrimary中
		supplementReservationPrimaryWithReservation(reservationPrimary);
		return reservationPrimary;
	}
















}
